#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
 
#include "data_type.h"
#include "alloc.h"
#include "memfunc.h"
#include "basefunc.h"
#include "struct_deal.h"
#include "crypto_func.h"
#include "memdb.h"
#include "message.h"
#include "ex_module.h"
#include "sys_func.h"
#include "tcm_constants.h"
#include "tcm_structures.h"
#include "tcmfunc.h"
#include "tcm_cube_struct.h"
#include "tcm_key_manage.h"
#include "tcm_key_desc.h"
#include "tcm_pik_desc.h"
#include "tcm_view.h"
#include "pik_get.h"
// add para lib_include
//
TCM_PUBKEY * pubek;
char * capubkeyfile="CApub.key";
RECORD(TCM_KEY_DESC,LOCAL_KEYSET) * local_keyset_get_byname (char * name);

int pik_get_init(void * sub_proc, void * para)
{
	int ret;

	// add yorself's module init func here
	return 0;
}
int pik_get_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	// add yorself's module exec func here


	// BYTE Buf1[DIGEST_SIZE*32];
    // RECORD(TCM_PIK_DESC,USERINFO)  * pik_userinfo1;
    // void * tcm_key_template1;
    // char userinfo_uuid1[DIGEST_SIZE*2+1];
    // DB_RECORD * db_record1;
    // RECORD(TCM_PIK_DESC,CADATA)  * pik_cadata1;
    // int info_len1;
    // // 找USERINFO  并生成USERINFO  uuid
    // RECORD(TCM_KEY_MANAGE,PRIVATE_KEY) * pik_info1;	
    // // // 找USERINFO  并生成USERINFO  uuid
	// char a[32];
	// Strcat(a,"zhaowen");
	// printf("a:%s",a);
    // db_record1=memdb_find_first(TYPE_PAIR(TCM_PIK_DESC,USERINFO),"username",a);
	// if(db_record1==NULL)
	// {
	// 	printf("找不到username\n");
	// }


	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
	
		// if((type==TYPE(TCM_PIK_DESC))&&(subtype==SUBTYPE(TCM_PIK_DESC,USERINFO)))
		// {
		// 	ret=proc_pik_info1(sub_proc,recv_msg);
		// }
        if((type==TYPE(MESSAGE))&&(subtype==SUBTYPE(MESSAGE,INSTANCE_INFO)))
		{
			ret=proc_pik_info3(sub_proc,recv_msg);
		}

		// if((type==TYPE(TCM_PIK_DESC))&&(subtype==SUBTYPE(TCM_PIK_DESC,USERINFO)))
		// {
		// 	ret=proc_pik_info1(sub_proc,recv_msg);
		// }
	}
	return 0;
}
int proc_pik_info1(void * sub_proc,void * recv_msg)
{
	BYTE Buf1[DIGEST_SIZE*32];
    RECORD(TCM_PIK_DESC,USERINFO)  * pik_userinfo1;
    void * tcm_key_template1;
    char userinfo_uuid1[DIGEST_SIZE*2+1];
    DB_RECORD * db_record1;
    RECORD(TCM_PIK_DESC,CADATA)  * pik_cadata1;
    int info_len1;
    // 找USERINFO  并生成USERINFO  uuid
    RECORD(TCM_KEY_MANAGE,PRIVATE_KEY) * pik_info1;	
    // // 找USERINFO  并生成USERINFO  uuid
	char a[32];
	Strcat(a,"zhaowen");
	printf("a:%s",a);
    db_record1=memdb_find_first(TYPE_PAIR(TCM_PIK_DESC,USERINFO),"username",a);
	if(db_record1==NULL)
	{
		printf("找不到username\n");
	}
    else
	{
		printf("找到username\n");
		pik_userinfo1=db_record1->record;
        tcm_key_template1=memdb_get_template(TYPE_PAIR(TCM_PIK_DESC,USERINFO));
        info_len1=struct_2_blob(pik_userinfo1,Buf1,tcm_key_template1);
        vtcm_ex_sm3(userinfo_uuid1,1,Buf1,info_len1);

        db_record1=memdb_find_first(TYPE_PAIR(TCM_PIK_DESC,CADATA),"userinfo_uuid",userinfo_uuid1);
        if(db_record1 == NULL)
		    {
		        printf("找不到uuid!\n");
		    }
	    else{
			    pik_cadata1=db_record1->record;
                db_record1=memdb_find_first(TYPE_PAIR(TCM_KEY_MANAGE,PRIVATE_KEY),"pubkey_uuid",pik_cadata1->pikpub_uuid);
                if(db_record1 == NULL)
		        {
		            printf("找不到uuid!\n");
		        }
	            else
				{
					pik_info1=db_record1->record;
	                printf("\n\n\npik_info1!!!!!!!!%s\n\n\n\n",pik_info1->uuid);
				}
	        }
	}
	return 0;
}
int proc_pik_info2(void * sub_proc,void * recv_msg)
{
	//DB_RECORD * db_record;
    //db_record=memdb_find_first(TYPE_PAIR(TCM_KEY_DESC,LOCAL_KEYSET),"user_name","zhaowen");
	//if(db_record != NULL)
	//{
	//	printf("找到了");
	//}
	//else
	//{
	//	printf("没找到");
	//}
	//return 0;
	char a[32];
	Strcat(a,"zhaowen");
	RECORD(TCM_KEY_DESC,LOCAL_KEYSET) * local_keyset;
	local_keyset=local_keyset_get_byname(a);
	if(local_keyset == NULL)
	{
		printf("没找到\n");
	}
	else
	{
		printf("找到了\n");
	}
	return 0;
}
RECORD(TCM_KEY_DESC,LOCAL_KEYSET) * local_keyset_get_byname (char * name)
{
	int ret;
	RECORD(TCM_KEY_DESC,LOCAL_KEYSET ) * local_keyset;
	DB_RECORD * db_record;
	char buf[DIGEST_SIZE];
	Memset(buf,0,DIGEST_SIZE);
	Strncpy(buf,name,DIGEST_SIZE);
	db_record=memdb_find_first(TYPE_PAIR(TCM_KEY_DESC,LOCAL_KEYSET),"user_name",buf);

	if(db_record==NULL)
		return NULL;
	return db_record->record;
}
int proc_pik_info3(void * sub_proc,void * recv_msg)
{
	int ret=0;
	int fd;
	RECORD(MESSAGE,INSTANCE_INFO)  * pik_userinfo;
	// RECORD(TCM_PIK_QUERY,PIK) * pik_query;
	void * new_msg;

	ret=message_get_record(recv_msg,&pik_userinfo,0);
	if(ret<0)
		return -EINVAL;
	if(pik_userinfo==NULL)
		return -EINVAL;

	char file_name[DIGEST_SIZE*4];
	BYTE * Buf1;
	Memset(file_name,0,DIGEST_SIZE*3);
	Strcpy(file_name,"pik_info/");
	Strcat(file_name,pik_userinfo->user_name);
	fd=open(file_name,O_RDONLY);
    Buf1=Talloc0(DIGEST_SIZE*32+1);
	ret=read(fd,Buf1,DIGEST_SIZE*32);
	close(fd);
	// printf("看这里：%s\n",Buf1);
	// printf("占用%d    \n",sizeof(* pik_userinfo));
	// pik_query=Talloc0(sizeof(* pik_query));
	// if(pik_query==NULL)
	// 	{
	// 		printf("分配失败\n");
	// 		return -ENOMEM;	

	// 	}
	// else{
	// 		printf("失败\n");
	// Strncpy(pik_query->username,pik_userinfo->username,DIGEST_SIZE);
	// printf("看这里：%s\n",pik_userinfo->username);
	// Memcpy(pik_query->user_role,pik_userinfo->user_role,DIGEST_SIZE);
	// printf("看这里：%s\n",pik_userinfo->user_role);
	// Memcpy(pik_query->pik_uuid,Buf1,DIGEST_SIZE);
	// }	
	
	// printf("看这里：%s\n",pik_userinfo->pik_uuid);

	// new_msg=message_create(TYPE_PAIR(TCM_PIK_DESC,USERINFO),recv_msg);	
	// if(new_msg==NULL)
	// 	return -EINVAL;

	// ret=message_add_record(new_msg,pik_userinfo);
	// if(ret<0)
	// 	return ret;	

	// new_msg=message_create(TYPE_PAIR(TCM_PIK_QUERY,PIK),recv_msg);	
	// if(new_msg==NULL)
	// 	return -EINVAL;

	// ret=message_add_record(new_msg,pik_query);
	// if(ret<0)
	// 	return ret;
	
	// ret=ex_module_sendmsg(sub_proc,new_msg);
	return 0;

	
}